const {User, validateUser} = require('../../model/user');
const bcrypt = require('bcrypt');
module.exports = async (req, res, next) => {
  const body = req.body;
  const {username, email, role, state} = body;
  const id = req.query.id;
  let user = await User.findOne({_id: id});
  let isValid = await bcrypt.compare(body.password, user.password);
  if(isValid) {
    // 密码比对成功,将数据更新到数据库中
    await User.updateOne({_id: id}, {
      username, email, role, state
    });
    res.redirect('/admin/user');
  }else{
    let obj = {
      path: '/admin/user-edit',
      message: '密码错误,不能修改信息',
      id: id
    }
    next(JSON.stringify(obj));
  }
}